package com.yizhang.disruptor.handler;

import com.lmax.disruptor.EventHandler;
import com.yizhang.disruptor.entity.OrderEvent;

/**
 * @Author: yi.zhang
 * @CreateTime: 2025-05-21 14:01:11
 * @Description: 订单事件处理器
 */
public class OrderEventHandler implements EventHandler<OrderEvent> {
    @Override
    public void onEvent(OrderEvent orderEvent, long sequence, boolean endOfBatch) throws Exception {
        // 模拟订单处理逻辑
        processOrder(orderEvent);
    }

    private void processOrder(OrderEvent orderEvent) {
        // 模拟订单支付逻辑
        System.out.println("处理订单: " + orderEvent.getOrderId() + ", 用户: " + orderEvent.getUserId() + ", 金额: " + orderEvent.getPrice());

        // 假设订单处理通过后更新订单状态
        orderEvent.setStatus("已支付");

        // 模拟库存扣减逻辑
        reduceInventory(orderEvent);

        System.out.println("订单处理完成: " + orderEvent.getOrderId() + " 状态: " + orderEvent.getStatus());
    }

    private void reduceInventory(OrderEvent orderEvent) {
        // 模拟库存扣减逻辑
        System.out.println("扣减库存, 订单: " + orderEvent.getOrderId());
    }
}
